package cn.iocoder.yudao.module.member.service.memberconf.confadditionalfee;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.confadditionalfee.vo.MemberConfAdditionalFeePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.confadditionalfee.vo.MemberConfAdditionalFeeSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confadditionalfee.MemberConfAdditionalFeeDO;
import cn.iocoder.yudao.module.member.dal.mysql.memberconf.confadditionalfee.MemberConfAdditionalFeeMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.CONF_POINT_CONF_ADDITIONAL_FEE_NOT_EXISTS;

/**
 * 阶梯费用配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MemberConfAdditionalFeeServiceImpl implements MemberConfAdditionalFeeService {

    @Resource
    private MemberConfAdditionalFeeMapper confPointConfAdditionalFeeMapper;

    @Override
    public Long createConfPointConfAdditionalFee(MemberConfAdditionalFeeSaveReqVO createReqVO) {
        // 插入
        MemberConfAdditionalFeeDO confPointConfAdditionalFee = BeanUtils.toBean(createReqVO, MemberConfAdditionalFeeDO.class);
        confPointConfAdditionalFeeMapper.insert(confPointConfAdditionalFee);
        // 返回
        return confPointConfAdditionalFee.getId();
    }

    @Override
    public void updateConfPointConfAdditionalFee(MemberConfAdditionalFeeSaveReqVO updateReqVO) {
        // 校验存在
        validateConfPointConfAdditionalFeeExists(updateReqVO.getId());
        // 更新
        MemberConfAdditionalFeeDO updateObj = BeanUtils.toBean(updateReqVO, MemberConfAdditionalFeeDO.class);
        confPointConfAdditionalFeeMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfPointConfAdditionalFee(Long id) {
        // 校验存在
        validateConfPointConfAdditionalFeeExists(id);
        // 删除
        confPointConfAdditionalFeeMapper.deleteById(id);
    }

    private void validateConfPointConfAdditionalFeeExists(Long id) {
        if (confPointConfAdditionalFeeMapper.selectById(id) == null) {
            throw exception(CONF_POINT_CONF_ADDITIONAL_FEE_NOT_EXISTS);
        }
    }

    @Override
    public MemberConfAdditionalFeeDO getConfPointConfAdditionalFee(Long id) {
        return confPointConfAdditionalFeeMapper.selectById(id);
    }

    @Override
    public PageResult<MemberConfAdditionalFeeDO> getConfPointConfAdditionalFeePage(MemberConfAdditionalFeePageReqVO pageReqVO) {
        return confPointConfAdditionalFeeMapper.selectPage(pageReqVO);
    }

}